package com.ly.mpayment.core.util;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.SystemDefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;

/**
 * 上传图片 发送请求
 *
 *
 */
@SuppressWarnings("deprecation")
public class HttpClient {

	private static String taobao = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";

	private static String baidu = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_name=guishudi&query=";

	public static String userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1";

	private static String juhedata = "http://apis.juhe.cn/mobile/get?phone=";

	private static String juheAppKey = "&key=2fffa92766f6843aa7fcbfcdec6b1195";


	/**
	 * 验证手机号码
	 * 发送get请求 中国联通 中国电信 中国移动
	 * {"mts":"1878243","province":"四川","catName":"中国移动","telString":"18782435842","areaVid":"30508","ispVid":"3236139","carrier":"四川移动"}
	 * @param url
	 *            路径
	 * @return
	 */
	public static JSONObject httpGet(String phoneNum) {
		// get请求返回结果
		JSONObject jsonResult = null;
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			// 发送get请求
			HttpGet request = new HttpGet(taobao.concat(phoneNum));
			HttpResponse response = client.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				String strResult = EntityUtils.toString(response.getEntity());
				strResult = strResult.replace("__GetZoneResult_ =", "");
				/** 把json字符串转换成json对象 **/
				jsonResult = JSONObject.fromObject(strResult);
				taobao = URLDecoder.decode(taobao, "UTF-8");
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		}
		return jsonResult;
	}

	public static com.alibaba.fastjson.JSONObject httpGetBaidu(String phoneNum) {
		// get请求返回结果
		com.alibaba.fastjson.JSONObject jsonResult = null;
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			// 发送get请求
			HttpGet request = new HttpGet(baidu.concat(phoneNum));
			HttpResponse response = client.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				String strResult = EntityUtils.toString(response.getEntity());
				strResult = strResult.replace("__GetZoneResult_ =", "");
				/** 把json字符串转换成json对象 **/
				jsonResult = com.alibaba.fastjson.JSONObject.parseObject(strResult);
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		}
		return jsonResult;
	}


	public static String httpGetUrl(String url) {
		// get请求返回结果
		try {
			// 发送get请求
			HttpGet request = new HttpGet(url);
//			trustAllHttpsCertificates();
			DefaultHttpClient httpclient = new SystemDefaultHttpClient();
			request.setHeader("User-Agent", userAgent);
			HttpResponse response = httpclient.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),HTTP.UTF_8);

			} else {
				System.out.println(response.getStatusLine().getStatusCode());
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}finally {
//			client.getConnectionManager().shutdown();
		}
	}

	//Authorization (REQUIRED)	string	签名认证信息，详见【签名生成】
	public static String httpGetWechatUrl(String url,String authorization) {
		// get请求返回结果
		try {
			// 发送get请求
			HttpGet request = new HttpGet(url);
			DefaultHttpClient httpclient = new SystemDefaultHttpClient();
			request.addHeader("User-Agent", userAgent);
			request.addHeader("Accept", "application/json");
			request.addHeader("Authorization",authorization);
			HttpResponse response = httpclient.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
			} else {
				System.out.println(response.getStatusLine().getStatusCode());
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}



	public static String httpGetJsessionId(String url,String phone) {
		// get请求返回结果
		try {
			// 发送get请求
			HttpGet request = new HttpGet(url);
//			trustAllHttpsCertificates();
			DefaultHttpClient httpclient = new SystemDefaultHttpClient();
			request.setHeader("User-Agent", userAgent);
			HttpResponse response = httpclient.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				for(Header h : response.getAllHeaders()){
					if(h.getName().equals("Set-Cookie")){
						//电信
						CookieRedis.addCookie(phone, h.getValue());
						break;
					}
				}
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),HTTP.UTF_8);

			} else {
				System.out.println(response.getStatusLine().getStatusCode());
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}finally {
//			client.getConnectionManager().shutdown();
		}
	}

	public static String httpGetUrlGbk(String url) {
		// get请求返回结果
		JSONObject jsonResult = null;
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			// 发送get请求
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),"gbk");

			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * {"resultcode":"200","reason":"Return Successd!","result":{"province":"广东","city":"深圳","areacode":"0755","zip":"18000","company":"移动","card":""},"error_code":0}
	 ////   {"resultcode":"200","reason":"Return Successd!","result":{"province":"四川","city":"成都","areacode":"028","zip":"610000","company":"电信","card":""},"error_code":0}
	 ////   {"resultcode":"200","reason":"Return Successd!","result":{"province":"四川","city":"成都","areacode":"028","zip":"610000","company":"联通","card":""},"error_code":0}
	 * 获取手机号归属地
	 * @param url
	 * @return
	 */
	public static JSONObject httpGetJuhe(String phoneNum) {
		// get请求返回结果
		JSONObject jsonResult = null;
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			// 发送get请求
			HttpGet request = new HttpGet(juhedata+phoneNum+juheAppKey);
			HttpResponse response = client.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				String strResult = EntityUtils.toString(response.getEntity());
				strResult = strResult.replace("__GetZoneResult_ =", "");
				/** 把json字符串转换成json对象 **/
				jsonResult = JSONObject.fromObject(strResult);
				//taobao = URLDecoder.decode(taobao, "UTF-8");
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		}
		return jsonResult;
	}


//	public static String httpGet(String url) {
//		// get请求返回结果
//		JSONObject jsonResult = null;
//		try {
//			DefaultHttpClient client = new DefaultHttpClient();
//			// 发送get请求
//			HttpGet request = new HttpGet(url);
//			HttpResponse response = client.execute(request);
//			/** 请求发送成功，并得到响应 **/
//			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//				/** 读取服务器返回过来的json字符串数据 **/
//				String strResult = EntityUtils.toString(response.getEntity());
//				/** 把json字符串转换成json对象 **/
//				jsonResult = JSONObject.fromObject(strResult);
//				url = URLDecoder.decode(url, "UTF-8");
//			} else {
//				return null;
//			}
//		} catch (IOException e) {
//			return null;
//		}
//		return jsonResult.toString();
//	}

//	static DefaultHttpClient httpclient = new SystemDefaultHttpClient();
	/**
	 * post 请求
	 *
	 * @Title post
	 * @Description TODO
	 * @param url
	 * @param params
	 * @return
	 */
	public static String post(String url, String data) {
		HttpPost post = postJson(url, data);
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		String body = invoke(httpclient, post);
		return body;
	}

	/**
	 * 微信下单
	 * @param url
	 * @param data
	 * @param authorization
	 * @return
	 */
	public static String postWechat(String url, String data,String authorization) {
		HttpPost post = postJson(url, data);
		post.addHeader("Accept", "application/json");
		post.addHeader("Content-type","application/json; charset=utf-8");
		post.addHeader("Authorization",authorization);
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		String body = invoke(httpclient, post);
		return body;
	}



	private static String invoke(DefaultHttpClient httpclient, HttpUriRequest httpost) {
		HttpResponse response = sendRequest(httpclient, httpost);
		String body = paseResponse(response);
		return body;
	}

	/**
	 * 结果转换
	 *
	 * @Title paseResponse
	 * @Description TODO
	 * @param response
	 * @return
	 */
	private static String paseResponse(HttpResponse response) {
		// String charset = EntityUtils.getContentCharSet(entity);
		String body = null;
		try {
			if(response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity entity = response.getEntity();
				body = EntityUtils.toString(entity,HTTP.UTF_8);
				return body;
			}
			//如果不等于200 则返回状态码
			return response.getStatusLine().getStatusCode()+"";
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return body;
	}

	/**
	 * 返回结果 HttpResponse
	 *
	 * @Title sendRequest
	 * @Description TODO
	 * @param httpclient
	 * @param httpost
	 * @return
	 */
	private static HttpResponse sendRequest(DefaultHttpClient httpclient, HttpUriRequest httpost) {
		HttpResponse response = null;
		try {
			response = httpclient.execute(httpost);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}

	/**
	 *  系统充值使用 上游
	 *
	 * @Title postForm
	 * @Description TODO
	 * @param url
	 * @param params
	 * @return
	 */
	private static HttpPost postJson(String url, String data) {
		HttpPost httpost = new HttpPost(url);
		httpost.setHeader("Content-Type", "application/json");
//		httpost.setHeader("Cookie", "ssologinprovince=280; inx=myorders; inx2=returnorderqry; cart_code_key=fku9ntei8nhrdp4lnkphipfd84; CmLocation=280|280; verifyCode=f3b273a641332f0118e9735f7d82f40f9b7b65ef; key4IE89=FF473A11D089026601702E68F5AC0C43EFF288FF39DFC1E298DEA5D32EAEBBB2740AFC7F56DA77655A7310BA0BE6365F; jsessionid-echd-cpt-cmcc-jt=B8F48FC40BF305626968EBB9E939CD1B; collect_id=63maqvpukm7nntkafqdhqbcrx5xnlmer; sendflag=20190703205152624365; lgToken=1e28188433da4aa88a1a49392384f856; CaptchaCode=AHAZui; cmccssotoken=1f2053f7860f4106abd3a8f605a153a4@.10086.cn; c=1f2053f7860f4106abd3a8f605a153a4; CmProvid=bj; chargeresource=s%3D~e%3D~c%3D~taskId%3D; PHPSESSID=vk629vn40v24p48n1vlo9tlfs0; mobile=; WT_FPC=id=2bab2820a74edfa0b541562081433368:lv=1562162413871:ss=1562156367675");
//		httpost.setHeader("Referer","https://shop.10086.cn/i/?f=rechargecredit&welcome=1562158304954");
//		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		try {
//			httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			httpost.setEntity(new StringEntity(data,HTTP.UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return httpost;
	}




	/**************postUrl*************/
	public static String postUrlFrom(String url,String data ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm(url, data);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	public static String postUrlFromLuhong(String url,String data ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm(url, data);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		post.setHeader("Accept", "application/json;charset=UTF-8");
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	public static String postUrlFromHeader(String url,String data, Map<String, String> headers ,String ip){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm(url, data);
		if(headers != null){
			for(String key : headers.keySet()){
				post.setHeader(key, headers.get(key));
			}
		}
		post.addHeader("x-forwarded-for",ip);
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

//	/**************postUrl*************/
	public static String postUrl(String url){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost httpost = new HttpPost(url);
		httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		String body = invoke(httpclient, httpost);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	public static String postUrl(String url,String data ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm(url, data);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}


	public static String postDianxin(String url,String data ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost httpost = new HttpPost(url);
		try {
			httpost.setEntity(new StringEntity(data,HTTP.UTF_8));
			String body = invoke(httpclient, httpost);
			httpclient.getConnectionManager().shutdown();
			return body;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String postXml(String url, String data) {
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm(url, data);
		post.setHeader("Content-Type", "application/xml");
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	public static String postXmlHeader(String url, String data,Map<String, String> headers) {
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm(url, data);
		if(headers != null){
			for(String key : headers.keySet()){
				post.setHeader(key, headers.get(key));
			}
		}
		post.setHeader("Content-Type", "application/xml");
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	public static String postJiTeng(String url,String data ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost httpost = new HttpPost(url);
		try {
			httpost.setEntity(new StringEntity(data,HTTP.UTF_8));
			httpost.setHeader("Content-Type", "text/xml");
			String body = invoke(httpclient, httpost);
			httpclient.getConnectionManager().shutdown();
			return body;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

/*********************************集团用户*******************************************/
//	public static String postGd(String url,String data ){
//		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
//		HttpPost post = postUrlEncodedForm(url, data);
//		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//		post.setHeader("Referer", "http://gd.10086.cn/group/login/loginIndex.jspx?lastPageUrlForLogin=%2Fgroup%2Fhome%2Findex.jspx");
//		setHeader(post);
//		String body = invoke(httpclient, post);
//		httpclient.getConnectionManager().shutdown();
//		return body;
//	}
//
//	public static String postNewAccount(String url,String data ,String HVCODECODE){
//		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
//		HttpPost post = postUrlEncodedForm(url, data);
//		post.setHeader("Content-Type", "application/json");
//		post.setHeader("Referer", "http://gd.10086.cn/group/bill/findBills/8590.jspx");
//		post.setHeader("H-VCODE-CODE", HVCODECODE);
//		setHeader(post);
//
//		String body = invoke(httpclient, post);
//		httpclient.getConnectionManager().shutdown();
//		return body;
//	}
//
//
//
//	public static void setHeader(HttpPost post){
////		post.setHeader("Accept-Encoding", "gzip, deflate");
////		post.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
//		post.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
//		post.setHeader("Connection", "keep-alive");
//		post.setHeader("Host", "gd.10086.cn");
//		post.setHeader("Origin", "chrome-extension://coohjcphdfgbiolnekdpbcijmhambjff");
//		//必须要
//		post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");
//		post.setHeader("X-Requested-With", "XMLHttpRequest");
//		post.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
//	}
//	static DefaultHttpClient client = new DefaultHttpClient();

	@SuppressWarnings("unused")
	public static String httpGet10086Url(String url,Map<String, String> headers) {
		// get请求返回结果
		JSONObject jsonResult = null;
		try {
			// 发送get请求
			HttpGet request = new HttpGet(url);
			if(headers != null && !headers.isEmpty()){
				for (String str : headers.keySet()) {
					request.setHeader(str, headers.get(str));
				}
			}
			DefaultHttpClient client = new SystemDefaultHttpClient();
			HttpResponse response = client.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				for (Header str :response.getAllHeaders()) {
//					System.out.println(str.getValue());
				}
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),HTTP.UTF_8);

			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 *  Set-Cookie: cmccssotoken=c338c4608c334fec82855197fcc990a4@.10086.cn;domain=.10086.cn;secure;HTTPOnly;
		Set-Cookie: is_login=true; domain=.10086.cn; path=/
		Set-Cookie: defaultloginuser_p=null; domain=.10086.cn; expires=Thu, 01-Jan-1970 01:00:00 GMT; path=/
		Set-Cookie: c=c338c4608c334fec82855197fcc990a4; domain=.10086.cn; path=/
		Set-Cookie: verifyCode=f3b273a641332f0118e9735f7d82f40f9b7b65ef; domain=.10086.cn; expires=Thu, 08-Aug-2019 16:07:20 GMT; path=/
		Set-Cookie: CITY_INFO=100|10;domain=.10086.cn;path=/;expires=Mon, 29-Jun-2015 06:11:17 GMT
	 * @param url
	 * @param headers
	 * @param json
	 * @return
	 */
	public static String httpGet10086Login(String url,Map<String, String> headers,String phoneNum ) {
		// get请求返回结果
		JSONObject jsonResult = null;
		try {
//			DefaultHttpClient client = new DefaultHttpClient();
			// 发送get请求
			HttpGet request = new HttpGet(url);
			if(headers != null && !headers.isEmpty()){
				for (String str : headers.keySet()) {
					request.setHeader(str, headers.get(str).toString());
				}
			}
			DefaultHttpClient client = new SystemDefaultHttpClient();
			request.setHeader("User-Agent", userAgent);
			HttpResponse response = client.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				CookieRedis.addCookie(phoneNum,response.getAllHeaders());
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),HTTP.UTF_8);

			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		}
	}

	public static String postUrl10086Form(String url,String data, Map<String, String> headers ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm10086(url, data);
//		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		if(headers != null){
			for(String key : headers.keySet()){
				post.setHeader(key, headers.get(key));
			}
		}
		post.setHeader("User-Agent", userAgent);
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}


	public static String post10086Json(String url,String data , Map<String, String> headers){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
//		trustAllHttpsCertificates();
		HttpPost post = post10086Json(url, data);
		post.setHeader("Content-Type", "application/json; charset=UTF-8");
		if(headers != null){
			for(String key : headers.keySet()){
				post.setHeader(key, headers.get(key));
			}
		}
		post.setHeader("User-Agent", userAgent);
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	public static String post10086Url(String url,String data , Map<String, String> headers){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
//		trustAllHttpsCertificates();
		HttpPost post = post10086Json(url, data);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		if(headers != null){
			for(String key : headers.keySet()){
				post.setHeader(key, headers.get(key));
			}
		}
		post.setHeader("User-Agent", userAgent);
		String body = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return body;
	}

	private static HttpPost post10086Json(String url, String data) {
		HttpPost httpost = new HttpPost(url);
		try {
			httpost.setEntity(new StringEntity(data,HTTP.UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return httpost;
	}

	private static HttpPost postUrlEncodedForm10086(String url, String data) {
		HttpPost httpost = new HttpPost(url);
		try {
//			List<NameValuePair> nvps = new ArrayList<>();
//			nvps.add(new BasicNameValuePair("userName", ""));
//			nvps.add(new BasicNameValuePair("type", "01"));
//			nvps.add(new BasicNameValuePair("channelID", "12034"));
//			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			httpost.setEntity(new StringEntity(data));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return httpost;
	}

	/**
	 * 添加参数
	 *
	 * @Title postForm
	 * @Description TODO
	 * @param url
	 * @param params
	 * @return
	 */
	private static HttpPost postUrlEncodedForm(String url, String data) {
		HttpPost httpost = new HttpPost(url);
		try {
			httpost.setEntity(new StringEntity(data,HTTP.UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return httpost;
	}

//	public static String httpGetCaptchaVal(String url) {
//		// get请求返回结果
//		JSONObject jsonResult = null;
//		try {
//			DefaultHttpClient client = new DefaultHttpClient();
//			// 发送get请求
//			HttpGet request = new HttpGet(url);
//			HttpResponse response = client.execute(request);
//			/** 请求发送成功，并得到响应 **/
//			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//
//				/** 读取服务器返回过来的json字符串数据 **/
//				return EntityUtils.toString(response.getEntity());
//
//			} else {
//				return null;
//			}
//		} catch (IOException e) {
//			return null;
//		}
//	}


	/**
	 * 支付宝路由
	 * @param url
	 * @param data
	 * @param headers
	 * @return
	 */
	@SuppressWarnings("resource")
	public static String postUrlAliPayForm(String url,String data, Map<String, String> headers,String orderId ){
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		HttpPost post = postUrlEncodedForm10086(url, data);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		if(headers != null){
			for(String key : headers.keySet()){
				post.setHeader(key, headers.get(key));
			}
		}
		post.setHeader("User-Agent", userAgent);

		HttpResponse response = null;
		try {
			response = httpclient.execute(post);
			String body = null;
			if(response != null){
				HttpEntity entity = response.getEntity();
//				Set-Cookie: JSESSIONID=3E782B33531ADBAE208A8E8C20EB685D; Path=/; HttpOnly
//				Set-Cookie: zone=RZ24B; Domain=.alipay.com; Path=/
//				Set-Cookie: ALIPAYJSESSIONID=RZ24kEnR22aE8d4m2NilQMcK08bSbTsuperapiRZ24; Domain=.alipay.com; Path=/
//				Set-Cookie: ctoken=-L3x7C9vlLyhN14n; Domain=.alipay.com; Path=/
//				Set-Cookie: spanner=9oPLq48wKXk/f9xJG+ZykXw8znxzcnEcXt2T4qEYgj0=;path=/;secure;

//				for(Header h : response.getAllHeaders()){
//					if(h.getName().equals("Set-Cookie")){
//						if(h.getValue().contains("JSESSIONID")){
//							CookieRedis.addCookie(orderId,"JSESSIONID",h.getValue().split(";")[0].split("=")[1]);
//						}
//						if(h.getValue().contains("ALIPAYJSESSIONID")){
//							CookieRedis.addCookie(orderId,"ALIPAYJSESSIONID",h.getValue().split(";")[0].split("=")[1]);
//						}
//						if(h.getValue().contains("ctoken")){
//							CookieRedis.addCookie(orderId,"ctoken",h.getValue().split(";")[0].split("=")[1]);
//						}
//						if(h.getValue().contains("spanner")){
//							CookieRedis.addCookie(orderId,"spanner",h.getValue().split(";")[0].split("=")[1]);
//						}
//						if(h.getValue().contains("zone")){
//							CookieRedis.addCookie(orderId,"zone",h.getValue().split(";")[0].split("=")[1]);
//						}
//					}
//				}
				if(response.getStatusLine().getStatusCode() == 302){
					for(Header h : response.getAllHeaders()){
						if(h.getName().equals("Location")){
							return httpGetAaliUrl(h.getValue(),CookieRedis.getHttpCookie(orderId),orderId);
						}
					}
				}else{
					body = EntityUtils.toString(entity,HTTP.UTF_8);
					return body;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpclient.getConnectionManager().shutdown();
		return null;
	}

	public static String httpGetAaliUrl(String url,String cookie,String orderId) {
		DefaultHttpClient httpclient = new SystemDefaultHttpClient();
		try {
			// 发送get请求
			HttpGet request = new HttpGet(url);

			request.setHeader("User-Agent", userAgent);
//			request.setHeader("Cookie", cookie);
			request.setHeader("Referer", "http://paygo.189.cn:9778/189pay/service");
			HttpResponse response = httpclient.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				return EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
			} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
				for(Header h : response.getAllHeaders()){
//					if(h.getValue().contains("JSESSIONID")){
//						CookieRedis.addCookie(orderId,"JSESSIONID",h.getValue().split(";")[0]);
//					}
					if(h.getName().equals("Location")){
						return httpGetAaliUrl(h.getValue(),cookie,orderId);
					}
				}
			} else {
				System.out.println(response.getStatusLine().getStatusCode());
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;
	}


}
